<?php

class FormSetEmail extends CFormModel {

    public $email;
    public $config = array();

    public function init() {
        $this->config = array(
            'class' => 'form',
            'elements' => array(
                'email' => array('type' => 'text'),
            ),
            'buttons' => array(
                'send' => array('type' => 'htmlSubmit', 'value' => 1, 'class' => 'blue', 'label' => '<span class="label">Сохранить</span>'),
            )
        );
        return parent::init();
    }

    public function rules() {
        return array(
            array('email', 'required'),
            array('email', 'email'),
            array('email', 'checkUE'),
        );
    }

    public function attributeLabels() {
        return array('email' => 'E-Mail');
    }

    public function checkUE($attribute, $params) {
        if(!$this->hasErrors()) {
            $user = User::model()->findByPk(Y::userId());
            $user->email = $this->email;
            $user->save();
        }
        return true;
    }
}
